package cn.qingyun.gis.modules.scenes.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import cn.qingyun.gis.modules.scenes.entity.PoiScenario;
import cn.qingyun.gis.modules.scenes.request.PoiDeleteRequest;
import cn.qingyun.gis.modules.scenes.request.PoiScenarioRequest;
import cn.qingyun.gis.modules.scenes.response.AddressResponse;
import cn.qingyun.gis.modules.scenes.response.TaskDetailResponse;
import cn.qingyun.gis.modules.scenes.service.IPoiScenarioService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: 表后台管理
 * @Version: V1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/admin/scenes/poi-street")
@Slf4j
public class PoiScenarioController extends BasicController<PoiScenario, IPoiScenarioService> {
    @Resource
    private IPoiScenarioService iPoiScenarioService;

    /**
     * 分页列表查询
     *
     * @param poiScenario
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "表-分页列表查询")
    @ApiOperation(value = "表-分页列表查询", notes = "表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PoiScenario poiScenario,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<PoiScenario> queryWrapper = QueryGenerator.initQueryWrapper(poiScenario, req.getParameterMap());
        Page<PoiScenario> page = new Page<>(pageNo, pageSize);
        IPage<PoiScenario> pageList = iPoiScenarioService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param poiScenario
     * @return
     */
    @AutoLog(value = "表-添加")
    @ApiOperation(value = "表-添加", notes = "表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PoiScenario poiScenario) {
        iPoiScenarioService.save(poiScenario);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param poiScenario
     * @return
     */
    @AutoLog(value = "表-编辑")
    @ApiOperation(value = "表-编辑", notes = "表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody PoiScenario poiScenario) {
        iPoiScenarioService.updateById(poiScenario);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "表-通过id删除")
    @ApiOperation(value = "表-通过id删除", notes = "表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iPoiScenarioService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "表-批量删除")
    @ApiOperation(value = "表-批量删除", notes = "表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iPoiScenarioService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "表-通过id查询")
    @ApiOperation(value = "表-通过id查询", notes = "表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        PoiScenario poiScenario = iPoiScenarioService.getById(id);
        if (poiScenario == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(poiScenario);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param poiScenario
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PoiScenario poiScenario) {
        return super.exportXls(request, poiScenario, PoiScenario.class, "表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PoiScenario.class);
    }

    /**
     * 通过id查询任务详情
     *
     * @param id
     * @return
     */

    @ApiOperation(value = "通过id查询任务详情", notes = "通过id查询任务详情")
    @GetMapping(value = "/queryTaskDetailById")
    public Result<?> queryTaskDetailById(@RequestParam(name = "id") String id) {
        TaskDetailResponse response = iPoiScenarioService.queryTaskDetailById(id);
        if (response == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(response);
    }


    /**
     * 通过gdId查询地址详情
     *
     * @param gdId
     * @return
     */
    @AutoLog(value = "通过gdId查询地址详情")
    @ApiOperation(value = "通过gdId查询地址详情", notes = "通过gdId查询地址详情")
    @GetMapping(value = "/queryAddressDetailByGdId")
    public Result<?> queryAddressDetailByGdId(@RequestParam(name = "gdId") String gdId) {
        AddressResponse response = iPoiScenarioService.queryAddressDetailByGdId(gdId);
        if (response == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(response);
    }
    /**
     * 新增或修改poi数据
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/poi/addOrUpdate")
    public Result<?> poiAddOrUpdate(@RequestBody @Validated PoiScenarioRequest request) {
        iPoiScenarioService.saveOrUpdatePoi(request);
        return Result.ok("poi添加或更新成功！");
    }
    /**
     * 删除poi数据
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/poi/delete")
    public Result<?> poiDelete(@RequestBody @Validated PoiDeleteRequest request) {
        iPoiScenarioService.poiDelete(request);
        return Result.ok("poi删除成功！");
    }

    /**
     * 季度数据更新接口
     * @return
     */
    @PostMapping(value = "/poi/quarter/update")
    public Result<?> poiQuarterUpdate() {
        iPoiScenarioService.poiQuarterUpdate();
        return Result.ok("poi季度更新成功！");
    }
}
